#coding:utf-8
import MySQLdb

def getConnect():
    #print "connection DB start..."
    #链接数据库
    try:
        db = MySQLdb.connect(
            host="127.0.0.1",
            port = 3306,
            user = "admin",
            passwd = "123456",
            db = "oms",
            charset='utf8'
            )
        return True, db 
    except Exception as e:
        #print e
        return False, e

        
def get_sku_for_Db():
    #获取订单信息
    db = getConnect()
    if db[0]:
        cursor = db[1].cursor()
    else:
        return "connection DB failed: {0}.".format(db[1])
    
    #汇总订单的状态，INIT：待审核，CHECK：已审核，DELIVERY：已出库
    sql = {"CHECK" : "SELECT COUNT(*) FROM sale_order WHERE `status`='CHECK' AND merchant_id=1 ;",
           "INIT" : "SELECT COUNT(*) FROM sale_order WHERE `status`='INIT' AND merchant_id=1 ;",
           "DELIVERY" : "SELECT COUNT(*) FROM sale_order WHERE `status`='DELIVERY' AND merchant_id=1 ;",
           "total" : "SELECT COUNT(*) FROM sale_order WHERE  merchant_id=1 ;"}

    order_total_num = {}
    try:
        for k,v in sql.items():
            cursor.execute(v)
            result = cursor.fetchall()#获取查询结果
            if result:
                for x in result:
                    for y in x:
                        order_total_num[k]=y
            else:
                return False
    except Exception as e:
        #如果发生异常，则回滚  
            print(u"发生异常", Exception, e)  
            db[1].rollback()  
    finally:  
        cursor.close()
        db[1].close()
    return order_total_num


if __name__ == "__main__":
	print get_sku_for_Db()